package com.sddz.pacs.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sddz.pacs.dto.PageModelParams;
import com.sddz.pacs.entity.SysRole;
import com.sddz.pacs.entity.SysRolePermission;
import com.sddz.pacs.mapper.SysRoleMapper;
import com.sddz.pacs.service.SysRolePermissionService;
import com.sddz.pacs.service.SysRoleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sddz.pacs.util.ShiroUtils;
import com.sddz.pacs.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 系统角色信息表 服务实现类
 * </p>
 *
 * @author 时代电子
 * @since 2018-05-16
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    SysRolePermissionService sysRolePermissionService;

    @Override
    public PageInfo<SysRole> selectListByCondition(PageModelParams params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        EntityWrapper<SysRole> ew = new EntityWrapper<>();
        //判断是否有查询条件
        if (params.getCondition() !=null){
            Object roleName = params.getCondition().get("roleName");//角色名称
            if(!StringUtils.isEmpty(roleName)){
                ew.like("ROLE_NAME" ,String.valueOf(roleName));
            }
            Object roleKey = params.getCondition().get("roleKey");//角色标识
            if(!StringUtils.isEmpty(roleKey)){
                ew.eq("ROLE_KEY" ,roleKey);
            }
            Object roleDescription = params.getCondition().get("roleDescription");//角色描述
            if(!StringUtils.isEmpty(roleDescription)){
                ew.eq("ROLE_DESCRIPTION" ,roleDescription);
            }
            Object status = params.getCondition().get("status");//状态
            if(!StringUtils.isEmpty(status)){
                ew.eq("STATUS" ,status);
            }
            Object remark = params.getCondition().get("remark");//备注字段
            if(!StringUtils.isEmpty(remark)){
                ew.eq("REMARK" ,remark);
            }
        }
        List<SysRole> roleList = this.baseMapper.selectList(ew);
        for(int i=0;i<roleList.size();i++){
            //查询该角色所拥有的权限标识
            Set<String> permission = sysRolePermissionService.getPermissionsByRoleId(roleList.get(i).getId());
            roleList.get(i).setPermissions(new ArrayList<>(permission));
        }
        return new PageInfo<>(roleList);
    }
    @Override
    /**
     * 添加角色名称
     * @param sysRole
     * @return boolean true= 成功， false = 失败
     */
    @Transactional
    public boolean addRole(SysRole sysRole){
        sysRole.setCreateDate(new Date());
        sysRole.setCreateUserid(ShiroUtils.getUserId());
        int length=this.baseMapper.insert(sysRole);
        if(length>0){
            sysRolePermissionService.saveRolePermission(sysRole);
        }
        return length>0?true:false;
    }
    @Override
    /**
     * 修改角色名称
     * @param sysRole
     * @return boolean true= 成功， false = 失败
     */
    @Transactional
    public boolean updateRole(SysRole sysRole){
        sysRole.setUpdateUserid(ShiroUtils.getUserId());
        sysRole.setUpdateDate(new Date());
        //先删除角色ID所有标识，在保存
        EntityWrapper<SysRolePermission> ew = new EntityWrapper<>();
        ew.eq("ROLE_ID",sysRole.getId());
        sysRolePermissionService.delete(ew);
        //保存用户授权
        sysRolePermissionService.saveRolePermission(sysRole);

        return this.baseMapper.updateById(sysRole) > 0 ? true : false;
    }

    @Override
    /**
     * 删除角色
     * @param sysRole
     * @return boolean true= 成功， false = 失败
     */
    @Transactional
    public boolean deleteRole(String id) {
        //删除角色用户id
        int length=this.baseMapper.deleteById(id);
        //如果删除成功，在删除角色对应的权限
        if(length>0){
            EntityWrapper<SysRolePermission> ew = new EntityWrapper<>();
            ew.eq("ROLE_ID",id);
            sysRolePermissionService.delete(ew);
        }
        return length>0?true:false;
    }

    /**
     * 根据用户ID查询角色
     * @param userId
     * @return
     */
    @Override
    public List<SysRole> getRolesByUserId(String userId) {
        List<SysRole> sysRoles = null;
        sysRoles = this.baseMapper.getRolesByUserId(userId);
        return sysRoles;
    }
}
